package com.srit.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

import com.srit.system.bean.DbTable;
import com.srit.system.dao.DbDao;

/**
 * 数据库dao层
 * 
 * @author guan
 * 
 */
public class DbDaoImpl implements DbDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<DbTable> queryTables() {
		String sql = "select * from user_tab_comments";
		return jdbcTemplate.query(sql, new DbTableRowMapper());
	}

	@Override
	public void queryTableDetail(DbTable dbTable) {
		String sql = "";
		dbTable.setDbColumns(jdbcTemplate.query(sql,
				new Object[] { dbTable.getTableName() },
				new DbColumnRowMapper()));

	}

}

class DbTableResultSetExtractor implements ResultSetExtractor {
	@Override
	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException {
		DbTable dbTable = new DbTable();
		dbTable.setTableName(rs.getString("TABLE_NAME"));
		dbTable.setRemarks("COMMENTS");
		return dbTable;
	}
}

class DbTableRowMapper implements RowMapper {
	@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		DbTableResultSetExtractor extractor = new DbTableResultSetExtractor();
		return extractor.extractData(rs);
	}
}

class DbColumnResultSetExtractor implements ResultSetExtractor {
	@Override
	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException {

		return null;
	}
}

class DbColumnRowMapper implements RowMapper {
	@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		DbColumnResultSetExtractor extractor = new DbColumnResultSetExtractor();
		return extractor.extractData(rs);
	}
}
